﻿using System.Linq;
using FluentNHibernate.Cfg;
using NHibernate.Cfg;
using NHibernate.Linq;
using NHibernate.Sidekick.Data.NHibernate;

namespace NHibernate.Sidekick.Services.Data
{
    /// <summary>
    /// Provides access to the NHibernate session.
    /// </summary>
    public static class NSession
    {
        /// <summary>
        /// Gets the <see cref="ISession"/>.
        /// </summary>
        /// <value>The <see cref="ISession"/>.</value>
        public static ISession Session
        {
            get { return SessionManager.Instance.GetSession(); }
        }

        /// <summary>
        /// Gets the <see cref="ISession"/>.
        /// </summary>
        /// <typeparam name="T">The domain object's type.</typeparam>
        /// <returns>The <see cref="IQueryable"/>.</returns>
        public static IQueryable<T> Query<T>() where T : class
        {
            return Session.Query<T>();
        }

        /// <summary>
        /// Gets the <see cref="ISession"/>.
        /// </summary>
        /// <typeparam name="T">The domain object's type.</typeparam>
        /// <returns>The <see cref="IQueryable"/>.</returns>
        [System.Obsolete("Use NSession.Query<T>()")]
        public static IQueryable<T> Linq<T>(this ISession session)
        {
            return session.Query<T>();
        }

        /// <summary>
        /// Sets the session factory.
        /// </summary>
        /// <param name="traditionalConfiguration">Standard XML (.hbm.xml) mapping configuration.</param>
        /// <param name="interceptor">The <see cref="IInterceptor"/>.</param>
        /// <returns>The main runtime interface between a .NET application and NHibernate.</returns>
        public static ISession SetSessionFactory(Configuration traditionalConfiguration, IInterceptor interceptor)
        {
            return SessionManager.Instance.SetSessionFactoryAndReturnSession(traditionalConfiguration, interceptor);
        }

        /// <summary>
        /// Sets the session factory.
        /// </summary>
        /// <param name="fluentConfiguration">Strongly typed C# code mapping configuration.</param>
        /// <param name="interceptor">The <see cref="IInterceptor"/>.</param>
        /// <returns>The main runtime interface between a .NET application and NHibernate.</returns>
        public static ISession SetSessionFactory(FluentConfiguration fluentConfiguration, IInterceptor interceptor)
        {
            return SessionManager.Instance.SetSessionFactoryAndReturnSession(fluentConfiguration, interceptor);
        }

        ///<summary>
        /// Clears the entire NHibernate cache. Useful when changes are done
        /// the database outside of the application's scope (i.e. done directly
        /// on the database or using a different application).
        ///</summary>
        public static void Evict_All_Objects_From_Cache()
        {
            SessionManager.Instance.Evict_All_Objects_From_Cache();
        }

        /// <summary>
        /// Evict all entries from the process-level cache.  This method occurs outside
        /// of any transaction; it performs an immediate "hard" remove, so does 
        /// not respect any transaction isolation semantics of the usage strategy. 
        /// Use with care.
        /// </summary>
        /// <param name="persistentClass">The persistent class.</param>
        public static void Evict_Persistent_Class(System.Type persistentClass)
        {
            SessionManager.Instance.Evict_Persistent_Class(persistentClass);
        }

        /// <summary>
        /// Evict an entry from the process-level cache. This method occurs outside
        /// of any transaction; it performs an immediate "hard" remove, so does 
        /// not respect any transaction isolation semantics of the usage strategy. 
        /// Use with care.
        /// </summary>
        /// <param name="persistentClass">The persistent class.</param>
        /// <param name="id"></param>
        public static void Evict_Persistent_Class(System.Type persistentClass, object id)
        {
            SessionManager.Instance.Evict_Persistent_Class(persistentClass, id);
        }
    }
}